package cn.com.dev;

import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import cn.com.kh.batch.FCallAPI_Utils;
import cn.com.kh.result.iAPIResult;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @Title: 法大大数据修复
 */
public class Dev_FixFDDData {
    private static final Log log = LogFactory.getLog(Dev_FixFDDData.class);
    private static JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());
    private static JSONObject res_Json = iAPIResult.ERROR("获取-个人用户信息查询 失败");

    public static void main(String[] args) throws Exception {

        // 获取一体化客户信息
        batchDBCCustFix();

        // 法大大客户基础信息
        batchFDDCustFix();

        // Fix-实名认证状态查询(个人&公司)
        // batchFix();

        // Fix-查询合同签署状态详情
        // batchContractSignFix();

        // Fix-临时导入数据检查法大大状态
        // tempFDDCustFix();

    }

    // Fix-海吉星客户基础信息
    public static void batchDBCCustFix() throws InterruptedException {
        String dealSQL = "Select R1.Rec_Guid, R1.Field_1 As Customerid From Ph_Contract_Sign_List R1 Left Join Sys_Oper_Log_Dbc R2 On R1.Rec_Guid = R2.Rec_Guid And R2.Status = 1 Where R2.Oper_Id Is Null";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-海吉星客户基础信息 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String Rec_Guid = KHUtil.GetMapV(CusMap, "Rec_Guid");
            String Customerid = KHUtil.GetMapV(CusMap, "Customerid");

            JSONObject get_Json = FCallAPI_Utils.getDBC_CustInfo(jt, Rec_Guid, Customerid);
            log.error("获取[" + Customerid + "][" + Rec_Guid + "]海吉星客户基础信息 结果：" + get_Json);

            Thread.sleep(200);

        }

    }

    // Fix-法大大客户基础信息
    public static void batchFDDCustFix() throws InterruptedException {
        String dealSQL = "Select R1.Rec_Guid, R1.Mobile From Sys_Oper_Log_Dbc R1 Left Join Sys_Oper_Account_Person R2 On R1.Rec_Guid = R2.Rec_Guid And R2.Status = 1 Where R1.Mobile Is Not Null And R1.Mobile != '' And R2.Oper_Id Is Null";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-法大大客户基础信息 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String Rec_Guid = KHUtil.GetMapV(CusMap, "Rec_Guid");
            String Mobile = KHUtil.GetMapV(CusMap, "Mobile");

            JSONObject get_Json = FCallAPI_Utils.getFDDPersonInfo(jt, Rec_Guid, Mobile);
            log.error("获取[" + Mobile + "][" + Rec_Guid + "]法大大客户基础信息 结果：" + get_Json);

            Thread.sleep(200);

        }

    }

    // Fix-实名认证状态查询(个人&公司)
    public static void batchFix() throws InterruptedException {
        String dealSQL = "select rec_guid,batch_id,created_by,created_date,name,account_no,file_title state,failed_resean,fdd_status,contract_id,template_id,property_number,fdd_customerid,department_id from ph_contract_sign_list where fdd_customerid is not null and fdd_customerid != '' and fdd_status = 1";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-实名认证状态查询(个人&公司) :" + selectCus.size());

        // 处理节点数据
        int CusCount = selectCus.size();
        for (Map<String, Object> CusMap : selectCus) {
            String rec_guid = KHUtil.GetMapV(CusMap, "rec_guid");
            String name = KHUtil.GetMapV(CusMap, "name");
            String fdd_customerid = KHUtil.GetMapV(CusMap, "fdd_customerid");

            JSONObject get_Json = FCallAPI_Utils.getCertificationStatus(jt, rec_guid, "个人", fdd_customerid);
            log.error("获取个人[" + name + "][" + rec_guid + "]认证状态 结果：" + get_Json);

            if (get_Json.getInt("code") == 0 && get_Json.getJSONObject("data").getJSONObject("data").getInt("verifiedStatus") == 2) {
                log.error("获取个人[" + name + "][" + rec_guid + "]认证状态 结果：" + get_Json.getJSONObject("data").getJSONObject("data").getString("verifiedStatusDesc"));
                jt.update("update ph_contract_sign_list set fdd_status = ?,failed_resean=?,created_by='S00001' where rec_guid = ?", get_Json.getJSONObject("data").getJSONObject("data").getInt("verifiedStatus"), get_Json.getJSONObject("data").getJSONObject("data").getString("verifiedStatusDesc"), rec_guid);
            }

            Thread.sleep(1000);

        }

    }

    // Fix-查询合同签署状态详情
    public static void batchContractSignFix() throws InterruptedException {
        String dealSQL = "Select R1.Rec_Guid, R1.Contract_Id From Ph_Contract_Sign_List R1 Left Join Sys_Oper_Log_Sign R2 On R1.Rec_Guid = R2.Rec_Guid And R2.Status = 1 Where R1.Contract_Id Is Not Null And R1.Contract_Id != '' And R2.Rec_Guid Is Null";
        dealSQL = "select contractlist_guid as Rec_Guid,contractid as Contract_Id from fdd_contract_list";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-查询合同签署状态详情 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String rec_guid = KHUtil.GetMapV(CusMap, "Rec_Guid");
            String contract_id = KHUtil.GetMapV(CusMap, "Contract_Id");

            JSONObject get_Json = FCallAPI_Utils.getContractSignStatus(jt, rec_guid, contract_id);
            log.error("查询合同签署状态详情[" + rec_guid + "] 结果：" + get_Json);

            Thread.sleep(100);

        }

    }

    // Fix-临时导入数据检查法大大状态
    public static void tempFDDCustFix() throws InterruptedException {
        String updateSQL = "Update Temp_Check_20240725 Set Customerid = ?, Verifiedstatus = ?, Verifiedstatusdesc = ?, Check_Flag = 1 Where F1 = ?";
        String dealSQL = "Select F1, 联系电话 From Temp_Check_20240725 Where Check_Flag Is Null Or Check_Flag = 0 Limit 100";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-法大大客户基础信息 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String Rec_Guid = KHUtil.GetMapV(CusMap, "F1");
            String Mobile = KHUtil.GetMapV(CusMap, "联系电话");

            JSONObject get_Json = FCallAPI_Utils.getFDDPersonInfo(jt, Rec_Guid, Mobile);
            log.error("获取[" + Mobile + "][" + Rec_Guid + "]法大大客户基础信息 结果：" + get_Json);

            String customerId = "";
            String verifiedStatus = "";
            String verifiedStatusDesc = "";

            if (KHUtil.GetSFJV(get_Json, "code").equals("1")) {
                if (KHUtil.GetSFJV(get_Json.getJSONObject("data"), "code").equals("1")) {
                    if (get_Json.getJSONObject("data").getJSONArray("data").size() > 0) {
                        customerId = KHUtil.GetSFJV(get_Json.getJSONObject("data").getJSONArray("data").getJSONObject(0), "customerId");
                        verifiedStatus = KHUtil.GetSFJV(get_Json.getJSONObject("data").getJSONArray("data").getJSONObject(0), "verifiedStatus");
                        verifiedStatusDesc = KHUtil.GetSFJV(get_Json.getJSONObject("data").getJSONArray("data").getJSONObject(0), "verifiedStatusDesc");
                    } else {
                        customerId = "";
                        verifiedStatus = "-1";
                        verifiedStatusDesc = "未注册";

                    }

                    jt.update(updateSQL, customerId, verifiedStatus, verifiedStatusDesc, Rec_Guid);
                } else {
                    customerId = "";
                    verifiedStatus = KHUtil.GetSFJV(get_Json.getJSONObject("data"), "code");
                    verifiedStatusDesc = KHUtil.GetSFJV(get_Json.getJSONObject("data"), "msg");
                    jt.update(updateSQL, customerId, verifiedStatus, verifiedStatusDesc, Rec_Guid);

                }
            }

            Thread.sleep(200);

        }

    }

}